<?php

class newestBannerWidgetComponents extends myComponents{

	public function executeRender(){
		$q = $this->getBannersQuery();
		$q = $this->setWidgetSpecificCriteria($q);

		$this->banner = $q->execute()->getFirst();
		if($this->banner)
			$this->bannerGroup = $this->banner->getBannerGroup();
		$this->gallery = $this->getGalleryNode();

		$this->section = $this->getRoute()->getObject();

		return $this->getAction()->renderPartial(
						'newestBannerWidget/render',
							$this->getVarHolder()->getAll()
						);
	}
	

	public function getGalleryNode(){
		return Doctrine::getTable('Structure')->findOneByParameterAndLang(
				'gallery',
				$this->getRoute()->getObject()->getLang()
		);
	}


	public function setWidgetSpecificCriteria(Doctrine_Query $q){
		$q->orderBy('bg.created_at desc, RANDOM()')
				->limit(1);
		return $q;

	}

	public function getBannersQuery(){
		return Doctrine_Query::create()
   			 ->select('b.*,bg.*')
   			 ->from('Banner b')
   			 ->innerJoin('b.BannerGroup bg')
   			 ->innerJoin("bg.StructureBannerGroups bgvs WITH bgvs.structure_id = ?",
				$this->getRoute()->getObject()->getId()	 )
			->where('bg.type = ?', 'default');
   			 
	}
}